package com.cht.commonUtil.requirements;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cht.commonUtil.webInit.SP;
import com.cht.commonUtil.widgets.Widgets;

public class ManipulateGridReq extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding( SP.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( SP.UIcodeSet );
		
		Connection conn = Widgets.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		int localSysNo = 0;
		
		String sql = "";
		String oper = request.getParameter("oper");
		String reqId = "";
		String reqSID = "";
		String result = "";
		String empId = request.getParameter("empId");
		String subsysId = request.getParameter("subsysId");
		
		try {
			localSysNo = Widgets.getLocalSysemId();
			
			/** 判別需進行何種操作 **/
			if ( "del".equalsIgnoreCase(oper) ) {
				reqId = request.getParameter("id");
				sql = "DELETE FROM reqs WHERE reqId=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, reqId);
				ps.execute();
				result = "刪除了"+reqId;
			} else if ( "add".equalsIgnoreCase(oper) ) {
				reqSID = request.getParameter("reqSId");
				String reqDesc = request.getParameter("reqDesc");
				int status = Integer.parseInt( request.getParameter("status") );
				sql = "INSERT INTO reqs (reqsid,reqdesc,status,creatorId,cDT,sysSN,subsysId) VALUES (?,?,?,?,?,?,?)";
				ps = conn.prepareStatement(sql);
				ps.setString(1, reqSID);
				ps.setString(2, reqDesc);
				ps.setInt(3, status);
				ps.setString(4, empId);
				ps.setTimestamp(5, Widgets.cDT());
				ps.setInt(6, localSysNo);
				ps.setString(7, subsysId);
				ps.execute();
				
				result = "新增完成 "+reqSID;
				
			} else if ( "edit".equalsIgnoreCase(oper) ) {
				int status = Integer.parseInt( request.getParameter("status") );
				reqId = request.getParameter("id");
				reqSID = request.getParameter("reqSId");
				String reqDesc = request.getParameter("reqDesc");
				sql = "UPDATE reqs SET reqsid=?,reqDesc=?,status=? WHERE reqId=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, reqSID);
				ps.setString(2, reqDesc);
				ps.setInt(3, status);
				ps.setString(4, reqId);
				ps.execute();
				
				result = "修改完成 "+reqSID;
			}
		} catch ( Exception e) {
			result = "異動"+reqSID+"失敗!!\n\n";
			result += Widgets.errLog(e);
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		response.getWriter().write(result);		
	}
}
